package com.fy.customview.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.fy.customview.R;
import com.fy.customview.bean.SettingData;
import com.fy.customview.utils.CalculationUtil;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        SettingData settingData = new SettingData();
        settingData.setAcceleration(-1);
        settingData.setAngle(0);
        settingData.setBankLength(100);
        settingData.setDistance0(100);
        settingData.setShipLength(10);
        settingData.setSonarCount(10);
        settingData.setVelocity0(10);
        simulationData3(settingData);
    }

    public void simulationData3(SettingData settingData) {
        /*
         * 1、读取配置的各项数据
         * 2、确定声呐信号发射的时间间隔
         * 3、计算每个声呐的位置
         * 4、计算经过每个时间间隔后船的位置，从而确定要给声呐的模拟数据
         * 5、定时发送数据给声呐控件
         * */
        //1、读取配置信息
        float y0 = settingData.getDistance0();//船的初始位置离岸边的距离
        float v0 = settingData.getVelocity0();
        float a = settingData.getAcceleration();
        float bankLength = settingData.getBankLength();
        float angle = settingData.getAngle();

        int sonarCount = settingData.getSonarCount();
        float shipLength = settingData.getShipLength();

        //2、确定声呐信号发射时间间隔，单位：s
        long dTime = 1;

        //3、计算每个声呐的位置
        final float dDistance = bankLength / sonarCount;
        final float[] sonarDistances = new float[settingData.getSonarCount()];//每个声呐的位置
        for (int i = 0; i < sonarDistances.length; i++) {
            sonarDistances[i] = i * dDistance;
        }

        //4、假设船一定能靠岸，计算出船是靠在x轴上还是y轴上
        double y = CalculationUtil.tan(bankLength, angle);
        double s;//靠岸需要走过的距离
        System.out.println("船的初始位置离岸边的距离:" + y0);
        System.out.println("船走到y轴需要走过的纵向距离:" + y);
        if (y > y0) {
            //靠x轴
            s = CalculationUtil.calculateHypotenuseByCos(y0, 90 - angle);//通过余弦计算斜边的边长
            System.out.println("船靠x轴总共走过的距离：" + s);
        } else {
            //靠y轴
            //负数为离岸，只能撞向y轴
            s = CalculationUtil.calculateHypotenuseByCos(bankLength, Math.abs(angle));
            System.out.println("船靠y轴总共走过的距离：" + s);
        }
        //如果当速度减到0的时候还没有靠岸则以这个时候的位移来计算
        double s0 = CalculationUtil.calculateDistanceWhenVTo0(v0, a);
        System.out.println("船减速到0时总共走过的距离：" + s0);
        if (s0 < s && s0 > 0) {
            s = s0;
        }
        //5、计算出船靠岸前每单位时间行驶的距离
        double st = 0;
        double t = 0;
        double sx = CalculationUtil.sin(s, 90 - angle);
        double shipLengthX = CalculationUtil.sin(shipLength, 90 - angle);
//        System.out.println("simulationData3->shipLength:" + shipLength + ",shipLengthX:" + shipLengthX);
//        System.out.println("simulationData3->st:" + st + ",s:" + s + ",s0:" + s0);
        while (st < s) {
            st = CalculationUtil.calculateDistance(v0, a, t);
            double sxt = CalculationUtil.sin(st, 90 - angle);
            System.out.println("船在时间" + t + "内走过的总距离：" + st);
            System.out.println("船在时间" + t + "内走过的x距离：" + sxt);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < sonarCount; i++) {
                if (sonarDistances[i] <= st && sonarDistances[i] > st - shipLengthX) {
                    double syt = (sx - sonarDistances[i]) / sx * y0;
                    if (angle == 0){
                        syt = y0;//0度y方向没有位移
                    }
//                    double syt = (sx - sxt) / sx * y0;
                    System.out.println("船在时间" + t + "内走过的y距离：" + syt);
                    stringBuilder.append(syt);
                    stringBuilder.append(",");
                } else {
                    stringBuilder.append("0,");
                }
            }
            final String data = stringBuilder.toString();
            System.out.println("声呐数据:" + t + " " + data);
            t += dTime;
//            System.out.println("simulationData3->while st:" + st + ",s:" + s);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.button2:
                startActivity(new Intent(this, SpringActivity.class));
                break;
            case R.id.button3:
                startActivity(new Intent(this, ChordTestActivity.class));
                break;
            case R.id.button4:
                startActivity(new Intent(this, TestFragmentActivity.class));
                break;
            case R.id.button5:
                startActivity(new Intent(this, ShowWidgetActivity.class));
                break;
            default:
                break;
        }
    }
}
